﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Management;
using Lib.Utils;

namespace Lib.Lic
{
    public class Licenses : IDisposable
    {
        public void Dispose()
        { }

        private LIC_Processor lic = new LIC_Processor();

        public LIC_Processor GetLicense()
        {
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(
                "Select * FROM WIN32_Processor");

            ManagementObjectCollection mObject = searcher.Get();

            foreach (ManagementObject obj in mObject)
            {
                lic.ProcessorId = obj["ProcessorId"].ToString();
                lic.SystemName = obj["SystemName"].ToString();
                lic.UniqueId = obj["UniqueId"].ToString();
                lic.Version = obj["Version"].ToString();
            }

            return lic;
        }

        public String GetCPUId()
        {
            String processorID = "";

            ManagementObjectSearcher searcher = new ManagementObjectSearcher(
                "Select * FROM WIN32_Processor");

            ManagementObjectCollection mObject = searcher.Get();

            foreach (ManagementObject obj in mObject)
            {
                processorID = obj["ProcessorId"].ToString() + ((char)35).ToString() +
                    Cvert.ToString(obj["SystemName"], "") + ((char)35).ToString() +
                    Cvert.ToString(obj["UniqueId"], "") + ((char)35).ToString() +
                    Cvert.ToString(obj["Version"], "");
            }

            mObject.Dispose();
            searcher.Dispose();

            return processorID;
        }

        public string GetOnlyID()
        {
            String processorID = "";
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(
                "Select * FROM WIN32_Processor");

            ManagementObjectCollection mObject = searcher.Get();

            foreach (ManagementObject obj in mObject)
            {
                processorID = obj["ProcessorId"].ToString();
            }

            mObject.Dispose();
            searcher.Dispose();

            return processorID;
        }
    }

    public class LIC_Processor
    {
        public string ProcessorId { get; set; }
        public string SystemName { get; set; }
        public string UniqueId { get; set; }
        public string Version { get; set; }
    }
}
